//fastclick.js
$(function() {
    FastClick.attach(document.body);
});
//侧边栏显示
$(".toggle-logo").on('click', function() {
    $(".mask").show();
    $(".slidebar-nav").removeClass('nav-close').addClass('nav--open');
    $('html').addClass('slidebar-nav--open');
});

$(".mask").click(function() {
    closeSlide();
});

$('#myTab a').click(function(e) {
    e.preventDefault()
    $(this).tab('show');
    var $title = $(this).find('span').html();
    $('.detail-header h1 .nav-title').html($title);
    closeSlide();
});

//关闭侧边栏导航
function closeSlide() {
    $(".mask").hide();
    $(".slidebar-nav").removeClass('nav--open');
    $('html').removeClass('slidebar-nav--open');
}

//全局返回按钮
$(".back-arrow").on('click', function() {
    window.location.href = "index.html";
});


//定义全局的userToken和timeStamp;
var userToken = window.localStorage.getItem('user_token') == null ? '' : window.localStorage.getItem('user_token');
var timeStamp = window.localStorage.getItem('time_stamp') == null ? '' : window.localStorage.getItem('time_stamp');

//登录信息显示
if(window.localStorage.getItem('user_name') != null) {
    $("#userName").html(window.localStorage.getItem('user_name')).attr('data-login','yes');
} else {
    $("#userName").html('登录').attr('data-login', 'no');
}

//全局状态下查看是否登录，未登录禁止设置
$(".set-btn").click(function(){
    if(userToken == '') {
        window.swal({
            title: '您未登录，请登录后进行设置！',
            text: '',
            type: 'warning',
            timer: 2000,
            showConfirmButton: false
        });
        return false;
    }
});



//提示框
$("[data-toggle='popover']").popover();


//用户登录判断
$(".user-login").click(function() {
    //当前状态下没有登录
    if ($(this).attr('data-login') == 'no') {
        window.location.href = "login.html";
    } 
});

//用户退出判断
$("#logout").click(function(){
    //在登录状态下退出
    if($('.user-login').attr('data-login') == 'yes') {
        logout();
    }
});

/**
 * 退出逻辑
 * @return {[type]} [description]
 */
function logout() {
    window.swal({
        title: '您确定要退出吗？',
        type: 'warning',
        showCancelButton: true,
        allowOutsideClick: false,
        allowEscapeKey: false
    }, function() {
        window.localStorage.removeItem('user_token');
        window.localStorage.removeItem('user_name');
        window.localStorage.removeItem('time_stamp');
        $("#userName").html('登录').attr('data-login', 'no');
        location.reload(true);
    });
}